package com.parenting.controller;


import cn.hutool.core.bean.BeanUtil;
import com.parenting.entities.OrdersDO;
import com.parenting.model.IMPage;
import com.parenting.oauth2.config.AuthUtil;
import com.parenting.request.RefundRequest;
import com.parenting.response.OrderDetailsResponse;
import com.parenting.response.OrderListResponse;
import com.parenting.response.RefuseRefundResponse;
import com.parenting.service.OrdersService;
import com.parenting.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表  前端控制器
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-09-18
 */
@RestController
@Api(tags = "审核通知", description = "退款退款 通知列表  退款详情")
@RequestMapping("/refund")
public class RefundReviewController {

    @Autowired
    private OrdersService ordersService;

    @ApiOperation("审核通知列表查询")
    @PostMapping("search")
    public Result<IMPage<OrderListResponse>> getRefundList(@RequestBody RefundRequest request){

        return Result.buildSuccess(ordersService.getRefundList(request, AuthUtil.getUserInfo().getShopId()));
    }

    @ApiOperation("同意退款")
    @GetMapping("agree/{orderId}")
    public Result ByRefund(@PathVariable("orderId") String orderId){
        ordersService.ByRefund(orderId);
        return  Result.buildSuccess();
    }


    @ApiOperation("拒绝退款")
    @PostMapping("refuse")
    public Result RefuseRefund(@RequestBody  RefuseRefundResponse refuseRefundResponse){

        ordersService.RefuseRefund(refuseRefundResponse);
        return  Result.buildSuccess();
    }



    @ApiOperation("查看审核通知订单详情")
    @ApiImplicitParam(name = "orderId", value = "订单ID", required = true)
    @GetMapping("{orderId}")
    public Result<OrderDetailsResponse> getOrderDetailsById(@PathVariable("orderId") String orderId){
        OrdersDO o = ordersService.getById(orderId);
        OrderDetailsResponse orderDetailsResponse = BeanUtil.copyProperties(o, OrderDetailsResponse.class);

        if (null==o.getOriginalPrice()){
            o.setOriginalPrice(0);
        }
        if (null==o.getItemConcessional()){
            o.setItemConcessional(0);
        }
        if (null==o.getItemNumber()){
            o.setItemNumber(0);
        }
        Integer  discount=(o.getOriginalPrice()-o.getItemConcessional())*o.getItemNumber();
        orderDetailsResponse.setDiscount(discount);
        return Result.buildSuccess(orderDetailsResponse);
    }
}
